package com.dg.river.module.common.utils;

import com.dg.river.core.http.ApiManager;
import com.dg.river.core.http.BeanCode;
import com.dg.river.core.http.BeanUtils;
import com.dg.river.core.http.OkGoUtils;
import com.dg.river.core.http.OnResultListener;
import com.dg.river.core.http.RequestCodeEnum;
import com.dg.river.core.util.BitmapUtils;
import com.dg.river.module.common.bean.UploadFileBean;
import com.dg.river.module.common.bean.UploadFileEnum;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：Hututu
 * @时间：2023/3/23 19:41
 * @描述：通用逻辑工具类
 * @修改：修改内容
 * @Version:1.0
 */
public class UploadFileUtils {

    private static UploadFileUtils uploadFileUtils;

    public static UploadFileUtils getInstance() {
        if (uploadFileUtils == null) {
            synchronized (UploadFileUtils.class) {
                if (uploadFileUtils == null) {
                    uploadFileUtils = new UploadFileUtils();
                }
            }
        }
        return uploadFileUtils;
    }

    public interface UploadResultListener {

        void onResult(UploadFileBean.DataDTO dataDTO);

        void onError(String msg);
    }

    public void uploadFile(String filePath, UploadFileEnum uploadFileEnum, UploadResultListener uploadResultListener) {
        Map<String, Object> map = new HashMap<>();
        map.put("fileType", uploadFileEnum.value);
        int degree = BitmapUtils.readPictureDegree(filePath);
        File file = new File(filePath);
        BitmapUtils.rotateImage(degree, file);
        List<File> fileList = new ArrayList<>();
        fileList.add(file);
        OkGoUtils.getInstance()
                .url(ApiManager.Other.POST_UPLOAD_FILE)
                .tag("上传单文件")
                .userToken()
                .paramsMap(map)
                .listFile("file", fileList)
                .executePost(new OnResultListener() {
                    @Override
                    public void onSuccess(String json) {
                        UploadFileBean uploadFileBean = BeanUtils.getInstance().convertBean(json, UploadFileBean.class, BeanCode.UPLOAD_FILE);
                        if (BeanUtils.getInstance().checkSuccess(uploadFileBean)) {
                            UploadFileBean.DataDTO data = uploadFileBean.getData();
                            if (data != null) {
                                if (uploadResultListener != null) {
                                    uploadResultListener.onResult(data);
                                } else {
                                    uploadResultListener.onError(null);
                                }
                            } else {
                                uploadResultListener.onError(null);
                            }
                        } else {
                            uploadResultListener.onError(null);
                        }
                    }

                    @Override
                    public void onError(RequestCodeEnum requestCodeEnum, String errorMsg) {
                        super.onError(requestCodeEnum, errorMsg);
                        uploadResultListener.onError(null);
                    }
                });
    }
}
